<?php
class Product_IndexController extends XPHP_Controller
{
	public function _init()
	{
		$this->loadLayout('/VietTrung_Detail');
	}

    public function indexAction()
    {
        //Lấy ra danh sách các sản phẩm mới nhất
        $model = new Areas_Product_Models_Product();
        $products = $model->getLastestProduct(12);
        $this->view->products = $products;
        $this->loadLayout("/VietTrung_Home");
        return $this->view();
    }

	public function categoryAction()
	{
		//Lấy danh sách sản phẩm thuộc danh mục + danh mục con.
		$modelProductCat = new Areas_Product_Models_Category();
		$modelProduct = new Areas_Product_Models_Product();
		$arrChildCat = $modelProductCat->getCategoryTree($this->params[0]);
		$modelProduct->db->where('category_id', $this->params[0]);
		foreach ($arrChildCat as $childCat)
		{
			$modelProduct->db->or_where('category_id', $childCat->id);
		}
		$arrProduct = $modelProduct->db->order_by('date_create', 'DESC')
								->get()
								->result();
		$category = $modelProductCat->db->where('id', $this->params[0])
											->get()
											->result();
		$this->view->category = $category[0];
		$this->view->arrProduct = $arrProduct;
		return $this->view();
	}
	
	public function detailAction()
	{
		//Lấy sản phẩm
		$modelProduct = new Areas_Product_Models_Product($this->params[0]);
		$this->view->product = $modelProduct;
		return $this->view();
	}
}